import 'package:enterprise_v6_app/app/components/custom_modele/custom_row_title.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/custom_bottom_sheet.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_time_period.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/access_record_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/closure/access_record/access_record_controller.dart';
import 'package:enterprise_v6_app/generated/assets.dart';

/// 人员审核
class PersonnelAccess extends StatelessWidget {
  PersonnelAccess({super.key});

  final controller = Get.put(AccessRecordController());

  // 报警状态
  Widget _status(int? i) {
    if(i == 0 || i == null) {
      return Container(
        padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 8.w),
        decoration: BoxDecoration(
            color: const Color(0xFF5C6DFF),
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(8.r),
              bottomRight: Radius.circular(8.r),
              topRight: Radius.circular(8.r),
            )),
        child: Text('正常',
            style: TextStyle(
              fontSize: 10.sp,
              color: Colors.white,
            )),
      );
    }
    return Container(
      padding: EdgeInsets.symmetric(vertical: 3.w, horizontal: 10.w),
      decoration: BoxDecoration(
          color: const Color(0xFFFD6B6B),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(8.r),
            bottomRight: Radius.circular(8.r),
            topRight: Radius.circular(8.r),
          )),
      child: Text('报警($i)',
          style: TextStyle(
            fontSize: 10.sp,
            color: Colors.white,
          )),
    );
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 36.w,
      height: 32.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 18.w,
        height: 16.w,
      ),
    ).clickOnce((){
      controller.screen(1);
    });
  }

  /// 获取右侧筛选样式
  Widget _getFilterWidget() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          //渐变位置
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          //渐变颜色[始点颜色, 结束颜色]
          colors: [
            Color(0xffE7EAFF),
            Colors.white,
            Colors.white,
            Colors.white,
          ],
        ),
      ),
      child: Column(
        children: [
          Expanded(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '筛选',
                        style: TextStyle(
                          fontSize: 18.sp,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                    AppGaps.vGap20,
                    AppGaps.vGap16,
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Text('人员类型',style: TextStyle(
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w400,
                        color: AppColors.font_color_black,
                      ),),
                    ),
                    AppGaps.vGap16,
                    Container(
                      height: 38.w,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(8.w),
                        border: Border.all(
                          color: AppColors.font_color_gray.withOpacity(0.3),
                          width: 1,
                        ),
                      ),
                      child: Row(
                        children: [
                          Expanded(
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 16.w),
                              child: Text(
                                controller.searchPersonTypeShow ?? '请选择人员类型',
                                style: TextStyle(
                                  fontSize: 14.sp,
                                  fontWeight: FontWeight.w400,
                                  color: AppColors.font_color_black,
                                ),
                              ),
                            ),
                          ),
                          Container(
                            width: 38.w,
                            height: 38.w,
                            alignment: Alignment.center,
                            child: SvgPicture.asset(
                              Assets.imagesIconFilterRightArrow,
                              width: 18.w,
                              height: 16.w,
                            ),
                          ),
                        ],
                      ),
                    ).click(selectedPersonType),
                    AppGaps.vGap16,
                    Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '培训时间',
                        style: TextStyle(
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                    AppGaps.vGap16,
                    CustomSelectTimePeriod(
                      startTime: controller.searchStartTime ?? '',
                      endTime: controller.searchEndTime ?? '',
                      onConfirm: (start, end) {
                        controller.searchStartTime = start;
                        controller.searchEndTime = end;
                      },
                      showBorder: true,
                      iconCalendar: AppImages.icon__screen_clock,
                    ),
                  ],
                ),
              )),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              OutlinedButton(
                onPressed: controller.reset,
                child: Text(
                  '重置',
                  style: TextStyle(fontSize: 16.sp, color: AppColors.font_color_blue),
                ),
              ),
              ElevatedButton(
                  style: ButtonStyle(backgroundColor: MaterialStateProperty.all(AppColors.font_color_blue)),
                  onPressed: () {
                    controller.confirm(1);
                  } ,
                  child: Text(
                    '查询',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 16.sp,
                    ),
                  )),
            ],
          ),
        ],
      )
    );
  }

  // 选择人员类型 人员类型 100.后台管理账号 1.企业人员账号 2.园区账号 3.政府人员账号 4:临时访客 5:第三方单位人员 6:其他
  selectedPersonType()  {
    CustomBottomSheet<Map<String, String>>(
      dataList: const [
        // {'name': '后台管理账号', 'value': '100'},
        // {'name': '企业人员账号', 'value': '1'},
        {'name': '园区账号', 'value': '2'},
        // {'name': '政府人员账号', 'value': '3'},
        {'name': '临时访客', 'value': '4'},
        {'name': '第三方单位人员', 'value': '5'},
        {'name': '其他', 'value': '6'},
      ],
      showValue: 'name',
      onConfirm: (value) {
        controller.searchPersonType = value['value'];
        controller.searchPersonTypeShow = value['name'];
        controller.update();
      },
    ).showBottomSheetDialog();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: controller.popRightScaffoldKeyPerson,
      backgroundColor: AppColors.bg_color_grey,
      endDrawer: Container(
        width: AppDimens.sw - 80.w,
        height: AppDimens.sh,
        color: Colors.white,
        child: _getFilterWidget(),
      ),
      body: Container(
        padding: EdgeInsets.symmetric(
          horizontal: 16.w,
        ),
        child: Column(
          children: [
            AppGaps.vGap14,
            Row(
              children: [
                Expanded(
                  child: CustomSearchEnter(
                    height: 38.w,
                    hintText: '请输入人员姓名',
                    text: controller.searchPersonName,
                    onCallBack: (v) {
                      controller.searchPersonName = v;
                      controller.refreshPage(1);
                    },
                  ),
                ),
                AppGaps.hGap8,
                _screen(),
              ],
            ),
            AppGaps.vGap16,
            Expanded(
                child: CustomEasyRefreshList<AccessRecordEntity>(
                  key: controller.globalKeyPerson,
                  dataLoad: (pageNum, pageSize, {search}) async {
                    return controller.checkAssetsDetails(pageNum, pageSize, 1, search: search);
                  },
                  itemBuilder: (item, index) {
                    return CustomTitleChartPage(
                      title: '${item.name}',
                      titleRightWidget: _status(item.numberOfViolations),
                      child: Column(
                        children: [
                          CustomRowTitle(
                            title: '人员类型:',
                            content: '${item.personnelCategory}',
                          ),
                          CustomRowTitle(
                            title: '所属企业:',
                            content: '${item.affiliatedUnit}',
                          ),
                          CustomRowTitle(
                            title: '最近入企时间:',
                            content: '${item.inDate}',
                          ),
                          CustomRowTitle(
                            title: '最近出企时间:',
                            content: '${item.outDate}',
                          ),
                        ],
                      ),
                    ).click(() {
                      controller.toDetails(item,1);
                    });
                  },
                ))
          ],
        ),
      ),
    );
  }
}
